package com.huangwei.socketchat.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

public class ClientSocketStart {
    private ExecutorService executorService;
    private static final String host = "127.0.0.1";
    private static final int port = 5463;
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        new ClientSocketStart().startClient();
    }

    public void startClient() throws IOException {
        executorService = Executors.newCachedThreadPool();
        Socket socket = new Socket(host, port);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf8"));
        System.out.println("请输入用户名:");
        String name = scanner.next();
        writer.write(name);
        writer.newLine();
        writer.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf8"));
        System.out.println(reader.readLine());
        ClientSocketThread clientSocketThread = new ClientSocketThread(socket);
        executorService.submit(clientSocketThread::serverMessagesLintenner);
        while (true) {
            System.out.println("输入要发生的内容:");
            String msg = scanner.next();
            writer.write(msg);
            writer.newLine();
            writer.flush();
            if (!"byebye".equals(msg)) break;
        }

    }
}
